<template>
  <el-card>
    <template #header>
      <h3>商品列表</h3>
    </template>
    <div>
      <!-- 表格 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="expand">
          <template v-slot="{ row }">
            <ul>
              <li>
                <span>商品ID</span>
                <p>{{ row.id }}</p>
              </li>
              <li>
                <span>商品名称</span>
                <p>{{ row.name }}</p>
              </li>
              <li>
                <span>所属分类</span>
                <p>{{ row.category }}</p>
              </li>
              <li>
                <span>商品价格</span>
                <p>{{ row.price }}</p>
              </li>
              <li>
                <span>创建时间</span>
                <p>{{ row.ctime | formateDate }}</p>
              </li>
              <li>
                <span>商品销量</span>
                <p>{{ row.sellCount }}</p>
              </li>
              <li>
                <span>商品评价</span>
                <p>{{ row.rating }}</p>
              </li>
              <li>
                <span>商品描述</span>
                <p>{{ row.goodsDesc }}</p>
              </li>
            </ul>
          </template>
        </el-table-column>
        <el-table-column
          label="商品名称"
          prop="name"
          width="280px"
        ></el-table-column>
        <el-table-column
          label="所属分类"
          prop="category"
          width="150px"
        ></el-table-column>
        <el-table-column label="商品价格" prop="price"></el-table-column>
        <el-table-column label="商品图片" width="190px">
          <template v-slot="{ row }">
            <img :src="basePicUrl + row.imgUrl" alt="" />
          </template>
        </el-table-column>
        <el-table-column
          label="商品描述"
          prop="goodsDesc"
          width="230px"
        ></el-table-column>
        <el-table-column label="操作" fixed="right" width="160px">
          <template v-slot="{ row }">
            <el-button size="mini" type="primary" @click="dialogVisible = true"
              >编辑</el-button
            >
            <el-button size="mini" type="danger" @click="del(row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[1, 5, 10, 15]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <!-- 编辑面板 -->
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <span>这是一段信息</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </el-card>
</template>

<script>
import { goodsList, delGoods } from '../../api/goods.js'
import _ from 'lodash'
import moment from 'moment'
export default {
  data() {
    return {
      pageSize: 5,
      currentPage: 1,
      total: 1,
      tableData: [],
      basePicUrl: 'http://127.0.0.1:5000/upload/imgs/goods_img/',
      dialogVisible: false
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.pageSize = val
      this.getGoodsList()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.currentPage = val
      this.getGoodsList()
    },
    async getGoodsList() {
      const { data, total } = await goodsList({
        currentPage: this.currentPage,
        pageSize: this.pageSize
      })
      // console.log(data, total)
      this.tableData = data
      this.total = total
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {})
    },
    // 查看当前行商品

    // 删除当前行商品
    del(id) {
      this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const data = await delGoods({ id })
          this.getGoodsList()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  },
  filters: {
    formateDate(str) {
      return moment(str).format('YYYY-MM-DD HH:mm:ss')
    }
  }
}
</script>

<style lang="less" scoped>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
img {
  width: 50px;
}
ul {
  width: 700px;
  display: flex;
  flex-wrap: wrap;

  li {
    width: 50%;
    display: flex;
    margin-bottom: 10px;

    span {
      width: 100px;
    }
  }
}
.el-pagination {
  margin-top: 20px;
}
</style>
